const Service = require('egg').Service;

var crypto = require('crypto');

class UtilService extends Service {

  async uuid(len, radix) {
    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
    var uuid = [], i;
    radix = radix || chars.length;
 
    if (len) {
      // Compact form
      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
    } else {
      // rfc4122, version 4 form
      var r;
 
      // rfc4122 requires these characters
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';
 
      // Fill in random data.  At i==19 set the high bits of clock sequence as
      // per rfc4122, sec. 4.1.5
      for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | Math.random()*16;
          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
      }
    }
 
    return uuid.join('');
  }


/**
 * 十六进制转换btye型
 * @param str
 * @returns {Promise}
 */
  Hexstring2btye (str) {
      let pos = 0;
      let len = str.length;
      if (len % 2 != 0) {
          return null;
      }
      len /= 2;
      let hexA = new Array();
      for (let i = 0; i < len; i++) {
          let s = str.substr(pos, 2);
          let v = parseInt(s, 16);
          hexA.push(v);
          pos += 2;
      }
      return hexA;
  }



  /**
 * byte型转换十六进制
 * @param b
 * @returns {string}
 * @constructor
 */
  Bytes2HexString (b) {
      let hexs = "";
      for (let i = 0; i < b.length; i++) {
          let hex = (b[i]).toString(16);
          if (hex.length === 1) {
              hexs = '0' + hex;
          }
          hexs += hex.toUpperCase();
      }
      return hexs;
  }


  /**
 * @Hmac-sha1加密模块 (每次加密随机,不可逆)
 * @param str string 要加密的字符串
 * @param secret string 要使用的加密密钥
 * @retrun string 加密后的字符串
 * */
    getHmac (str, secret) {
        
        //var buf = crypto.randomBytes(16);
        //secret = secret.toString("hex");//密钥加密；

        //secret = this.Bytes2HexString(secret);

        var Signture = crypto.createHash("sha1");//定义加密方式

        Signture.update(secret);


        // var cc = Signture.digest("hex");//.toString("hex");

        // //Signture.update(str);


        // for(var i = 1; i < 1024; i++){
        //     Signture.reset();

        //     //Signture.update(str);//.toString("hex");
        //     Signture.digest(str);
        // }

        // console.log('cc 1024: ' + cc );
        //var miwen = Signture.digest(str);

        //console.log('miwen 1: ' + miwen );

        

       

        //miwen.toString("hex");//生成的密文后将再次作为明文再通过pbkdf2算法迭代加密；

        return Signture.digest("hex");
    }


    byteToString(arr) {  
        if(typeof arr === 'string') {  
            return arr;  
        }  
        var str = '',  
            _arr = arr;  
        for(var i = 0; i < _arr.length; i++) {  
            var one = _arr[i].toString(2),  
                v = one.match(/^1+?(?=0)/);  
            if(v && one.length == 8) {  
                var bytesLength = v[0].length;  
                var store = _arr[i].toString(2).slice(7 - bytesLength);  
                for(var st = 1; st < bytesLength; st++) {  
                    store += _arr[st + i].toString(2).slice(2);  
                }  
                str += String.fromCharCode(parseInt(store, 2));  
                i += bytesLength - 1;  
            } else {  
                str += String.fromCharCode(_arr[i]);  
            }  
        }  
        return str;  
    }  
}

module.exports = UtilService;







